<template>
  <div class="mod-user">
    <el-button plain @click="Withdrawal">提现管理</el-button>
    <el-button plain disabled>提现记录</el-button>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <div class="seacrhMoney">
        <el-form-item>
          <div class="balance-searchMoney">
            提现申请单号
            <el-input class="input" v-model="dataForm.userName" placeholder="请输入提现申请单号"></el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="balance-searchMoney">
            提现申请时间
            <el-date-picker v-model="value" :clearable='false' type="daterange" value-format="yyyy-MM-dd HH:mm:ss" class="input" start-placeholder="开始日期" end-placeholder="结束日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="balance-searchMoney">
            打款时间
            <el-date-picker v-model="value4" :clearable='false' type="daterange" value-format="yyyy-MM-dd HH:mm:ss" class="input" start-placeholder="开始日期" end-placeholder="结束日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="balance-searchMoney">
            提现状态
            <el-select v-model="status" placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @click="demand">查询</el-button>
        <el-button type="primary" @click="empty">清空</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <div class="export">
      <div style="display: flex; justify-content: flex-end;margin-bottom:10px;">
        <el-button type="primary" @click="excel">导出Excel</el-button>
      </div>
      <el-table :data="dataList" v-loading="dataListLoading" element-loading-text="数据加载中" element-loading-spinner="el-icon-loading" element-loading-custom-class="loading_color" style="width: 100%;">
        <template slot="empty">
          <img class="machine" src="../../assets/img/machine.svg" alt="" />
          <div style="font-size: 14px; text-align: center">暂无数据</div>
        </template>
        <el-table-column prop="applyNo" header-align="center" align="center" label="提现申请单号" width="150">
        </el-table-column>
        <el-table-column prop="applyTime" header-align="center" align="center" label="提现申请时间" width="180">
        </el-table-column>

        <el-table-column prop="cycleNos" header-align="center" align="center" label="提现申请单位" width="150">
          <template slot-scope="scope">
            <div>
              <div>
                {{ scope.row.legalCompanyName }}
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="accountNickname" header-align="center" align="center" label="提现申请人" width="180">
        </el-table-column>
        <el-table-column prop="cycleNos" header-align="center" align="center" label="结算单号" width="150" :render-header="renderHeader">
          <template slot-scope="scope">
            <div v-for="(title, index) in scope.row.cycleNos" :key="index" class="cycleNos">
              <div @click="addOrUpdateHandles(title)">{{ title }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="applyFee" header-align="center" align="center" label="提现金额(元)" width="150">
          <template slot-scope="scope">
            <div @click="addMoney(scope.row.cycleNos)" class="cycleNos">
              {{ scope.row.applyFee| MoneyFormat }}
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="auditTime" header-align="center" align="center" label="提现审批时间" width="180">
        </el-table-column>
        <el-table-column prop="approverCompanyName" header-align="center" align="center" label="审批单位">
        </el-table-column>
        <el-table-column prop="approverFinanceName" header-align="center" align="center" label="审批人">
        </el-table-column>
        <el-table-column prop="applyStatus" header-align="center" align="center" width="150" label="提现状态">
          <template slot-scope="scope">
            <div v-if="scope.row.applyStatus === 0" class="orangeMoney">
              提现审批中
            </div>
            <div v-if="scope.row.applyStatus === 1" class="greenMoney">
              完成提现
            </div>
            <div v-if="scope.row.applyStatus === 2" class="grayMoney">
              <el-tooltip class="item" effect="dark" :content="scope.row.rejectReason" placement="top-start">
                <div class="newTitle">
                  <div class="grayMoneys">提现审批未通过</div>
                  <i name="wenhao" class="el-icon-question wenhao"></i>
                </div>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="orderNo" header-align="center" align="center" label="打款单号" width="150">
        </el-table-column>
        <el-table-column prop="agentTime" header-align="center" align="center" label="打款时间" width="180">
        </el-table-column>
        <el-table-column prop="bankAccountName" header-align="center" align="center" label="银行名称">
        </el-table-column>
        <el-table-column prop="bankCardCode" header-align="center" align="center" label="银行账号" width="120">
        </el-table-column>
        <el-table-column prop="actualFee" header-align="center" align="center" label="打款金额">
          <template slot-scope="scope">
            <div class="">{{ scope.row.actualFee| MoneyFormat }}</div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
import { formatDate } from "../../utils/time.js";
export default {
  data () {
    return {
      dataForm: {
        userName: "",
      },
      applyNo: "",
      applyTimeStart: "",
      applyTimeEnd: "",
      legalAccountCode: "",
      agentTimeStart: "",
      agentTimeEnd: "",
      dataList: [],
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      dataListLoading: false,
      userCode: "",
      value1: "",
      value: "",
      value2: "",
      value4: "",
      city: [],
      status: "",
      options: [
        {
          value: "0",
          label: "提现审核中",
        },
        {
          value: "1",
          label: "提现审核通过",
        },
        {
          value: "2",
          label: "提现审核不通过",
        },

      ],
    };
  },
  activated () {
    this.userCode = localStorage.getItem("userCode");
    this.cycleNo = this.$route.query.cycleNo;
    if (this.cycleNo) {
      this.init();
    } else {
      this.getDataList();
    }
    if (!localStorage.getItem("order_hover_num")) {
      localStorage.setItem("order_hover_num", 1);
      window.location.reload();
    }
  },
  deactivated () {
    localStorage.removeItem("order_hover_num");
  },
  methods: {
    // 表格头部提示事件
    renderHeader (h, { column }) {
      return h("span", [
        h("span", column.label),
        h(
          "el-tooltip",
          {
            props: {
              effect: "dark",
              content: "本次提现，申请的是哪条结算账单",
              placement: "top",
            },
          },
          [
            h("i", {
              class: "el-icon-question",
              style: "color:#000;margin-left:5px;",
            }),
          ],
          {
            content: "本次提现，申请的是哪条结算账单",
          }
        ),
      ]);
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl(
          "cassadmin/balanceDetail/getCityWithdrawDetailList"
        ),
        method: "get",
        params: this.$http.adornParams({
          cycleNo: this.cycleNo,
          applyNo: this.applyNo,
          applyTimeStart: this.applyTimeStart,
          applyTimeEnd: this.applyTimeEnd,
          legalAccountCode: this.legalAccountCode,
          agentTimeStart: this.agentTimeStart,
          agentTimeEnd: this.agentTimeEnd,
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          applyStatus: this.applyStatus
        }),
      }).then(({ data }) => {
        if (data.success == 1) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
          console.log(data.data);
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    //查询
    demand () {
      this.pageIndex = 1
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl(
          "cassadmin/balanceDetail/getCityWithdrawDetailList"
        ),
        method: "get",
        params: this.$http.adornParams({
          cycleNo: this.cycleNo,
          applyNo: this.dataForm.userName,
          applyTimeStart: this.value[0] || '',
          applyTimeEnd: this.value[1] || '',
          legalAccountCode: this.legalAccountCode,
          agentTimeStart: this.value4[0] || '',
          agentTimeEnd: this.value4[1] || '',
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          applyStatus: this.status
        }),
      }).then(({ data }) => {
        if (data.success == 1) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
          console.log(data.data);
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false
      });
    },
    //清空
    empty () {
      this.dataForm.userName = ''
      this.value = ''
      this.value4 = ''
      this.status = ''
      this.getDataList();
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl(
          "cassadmin/balanceDetail/getCityWithdrawDetailList"
        ),
        method: "get",
        params: this.$http.adornParams({
          cycleNo: this.cycleNo,
          applyNo: this.dataForm.userName,
          applyTimeStart: this.value[0] || '',
          applyTimeEnd: this.value[1] || '',
          legalAccountCode: this.legalAccountCode,
          agentTimeStart: this.value4[0] || '',
          agentTimeEnd: this.value4[1] || '',
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          applyStatus: this.status
        }),
      }).then(({ data }) => {
        if (data.success == 1) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
          console.log(data.data);
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = true;
      });
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val;
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl(
          "cassadmin/balanceDetail/getCityWithdrawDetailList"
        ),
        method: "get",
        params: this.$http.adornParams({
          cycleNo: this.cycleNo,
          applyNo: this.dataForm.userName,
          applyTimeStart: this.value[0] || '',
          applyTimeEnd: this.value[1] || '',
          legalAccountCode: this.legalAccountCode,
          agentTimeStart: this.value4[0] || '',
          agentTimeEnd: this.value4[1] || '',
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          applyStatus: this.status
        }),
      }).then(({ data }) => {
        if (data.success == 1) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
          console.log(data.data);
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // // 查看单号
    // addOrUpdateHandle(cycleNo) {
    //   this.$router.push({
    //     name: `subFinance-settlementList`,
    //     query: {
    //       cycleNo: cycleNo,
    //     },
    //   });
    // },
    // 查看单号
    addOrUpdateHandles (cycleNo) {
      this.$router.push({
        name: `subFinance-settlementList`,
        query: {
          cycleNo: cycleNo,
        },
      });
    },
    // 申请提现单位
    cityCode (e) {
      this.pageIndex = 1;
      this.legalAccountCode = e;
      this.getDataList();
    },
    // 提现管理
    Withdrawal () {
      this.$router.push({
        name: `subFinance-balanceDetails`,
      });
    },
    // 全部单号
    addMoney (cycleNos) {
      console.log(cycleNos);
      this.$router.push({
        name: `subFinance-settlementDetail`,
        query: {
          cycleNos: cycleNos,
        },
      });
    },
    init () {
      this.$http({
        url: this.$http.adornUrl(
          "cassadmin/balanceDetail/getCityWithdrawDetail"
        ),
        method: "get",
        params: this.$http.adornParams({
          cycleNo: this.cycleNo,
        }),
      }).then(({ data }) => {
        if (data.success == 1) {
          console.log(data);
          this.dataList.push(data.data);
          this.totalPage = 1;
          if (this.dataList.applyTime) {
            this.dataList.applyTime = formatDate(
              this.dataList.applyTime,
              "yyyy-MM-dd hh:mm"
            );
          }
          if (this.dataList.auditTime) {
            this.dataList.auditTime = formatDate(
              this.dataList.auditTime,
              "yyyy-MM-dd hh:mm"
            );
          }
          if (this.dataList.agentTime) {
            this.dataList.agentTime = formatDate(
              this.dataList.agentTime,
              "yyyy-MM-dd hh:mm"
            );
          }
        } else {
          this.$message.error(data.errorMsg);
        }
      });

      this.visible = true;
    },
    // 导出excel
    excel () {
      //applyNo
      if (this.value == '') {
        var valuestart = this.value
        var valueend = this.value
      } else {
        var valuestart = this.value[0]
        var valueend = this.value[1]
      }
      if (this.value4 == '') {
        var value4start = this.value4
        var value4end = this.value4
      } else {
        var value4start = this.value4[0]
        var value4end = this.value4[1]
      }
      window.location.href =
        `https://api.xstarlife.com/cassadmin/balanceDetail/downloadAllCityWithdrawDetail?userType=1&pageNum=1&pageSize=100&agentTimeStart=${value4start}&agentTimeEnd=${value4end}&legalAccountCode=${this.legalAccountCode}&applyTimeStart=${valuestart}&applyTimeEnd=${valueend}&applyNo=${this.dataForm.userName}&userCode=${this.userCode}&applyStatus=${this.status}`
    },
  },
};
</script>
<style scoped>
.input {
  flex: 1;
  margin-left: 5px;
}

.balance-searchMoney {
  display: flex;
  align-items: center;
}
.export {
  margin-top: 30px;
  background-color: #fff;
  padding: 20px;
  border-radius: 6px;
}
.grayMoneys {
  margin-right: 5px;
}
.wenhao {
  color: #000;
}
</style>
